Javascript replace space with regex


\s : means "one space"

\s+ : means "one or more spaces"

let stringWithSpace = '  A B  C   D EF ';

console.log(str.replace(/\s/g, '#'));                                
// ##A#B##C###D#EF# => means one space replace by one #;

console.log(str.replace(/\s+/g, '#'));                             
// #A#B#C#D#EF#  => means one or more spaces, all replace by #;
#javascript #replace #RegEx






你可能感興趣的文章

FB v.s. IG: 簡單的AB testing(Paired T-test)

FB v.s. IG: 簡單的AB testing(Paired T-test)

Day 39 & 40 - Flight Deal Finder [BIG project]

Day 39 & 40 - Flight Deal Finder [BIG project]

清晰說明針孔相機的內部參數與外部參數矩陣

清晰說明針孔相機的內部參數與外部參數矩陣






留言討論